package com.sansui.admin.core.ctrl;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.sansui.admin.common.ctrl.BaseCtrl;
import com.sansui.admin.core.service.LoginService;

@RestController
@ControllerAdvice
public class LoginController extends BaseCtrl implements ErrorController {

	@Autowired
	private LoginService loginService;

	@RequestMapping(value = "/index")
	public Object index() {
		return loginService.index();
	}

	@RequestMapping(value = "/login")
	public Object loginPost(String username, String password) {
		System.out.println(username);
		return loginService.loginPost(username, password);
	}

	@RequestMapping(value = "/logoutJSON")
	public Object logoutJSON() {
		return loginService.logoutJSON();
	}

	@RequestMapping(value = ERROR_PATH)
	public Object getStatus(HttpServletRequest request) {
		return loginService.getStatus(request);
	}

	@Override
	public String getErrorPath() {
		return ERROR_PATH;
	}

	@ExceptionHandler(value = UnauthorizedException.class)
	public Object unauth() {
		return loginService.unauth();
	}
}
